package com.lx.service.impl;

import com.lx.api.response.PrintTaskInfo;
import com.lx.dao.PrintOrderInfoMapper;
import com.lx.dao.PrintSystemParamMapper;
import com.lx.dao.PrintTaskDetailMapper;
import com.lx.dao.PrintTaskMapper;
import com.lx.model.PrintOrderInfo;
import com.lx.model.PrintSystemParam;
import com.lx.model.PrintTask;
import com.lx.model.PrintTaskDetail;
import com.lx.service.PrintTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PrintTaskServiceImpl implements PrintTaskService {
    @Autowired
    private PrintSystemParamMapper printSystemParamMapper;

    @Autowired
    private PrintTaskMapper printTaskMapper;

    @Autowired
    private PrintTaskDetailMapper printTaskDetailMapper;

    @Autowired
    private PrintOrderInfoMapper printOrderInfoMapper;

    @Override
    public List<PrintTaskInfo> selectPrintTask(Long shopId) {
        PrintSystemParam param = printSystemParamMapper.selectByShopId(shopId);
        int orderMin = 60;
        if (param!=null&&param.getPrintOrderMin()!=null)orderMin=param.getPrintOrderMin();
        List<PrintTaskInfo> list = new ArrayList<>();
        List<PrintTask> tl = printTaskMapper.selectForPrint(shopId,new Date(System.currentTimeMillis()-60*60*1000));

        if(tl!=null&&tl.size()>0){
            PrintTaskInfo printTaskInfo;
            for(PrintTask task:tl){
                printTaskInfo = new PrintTaskInfo();
                printTaskInfo.setPrintTask(task);
                List<PrintTaskDetail> detailList = printTaskDetailMapper.selectForPrint(task.getId());
                printTaskInfo.setDetails(detailList);
                PrintOrderInfo orderInfo = printOrderInfoMapper.selectByOrderSn(shopId,task.getOrderSn());
                printTaskInfo.setOrderInfo(orderInfo);
                list.add(printTaskInfo);


            }
        }
        return list;
    }

    @Override
    public void savePrintTaskStatus(Long taskId, String status) {
        PrintTask task = printTaskMapper.selectByPrimaryKey(taskId);
        task.setUpdateTime(new Date());
        task.setPrintStatus(status);
        printTaskMapper.updateByPrimaryKey(task);

    }
}
